package com.bcxj.servicedriveruser.controller;

import com.bcxj.internalcommon.constant.DriverCarConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.DriverCarBindingRelationship;
import com.bcxj.internalcommon.entity.DriverUser;
import com.bcxj.internalcommon.response.DriverUserExistResponse;
import com.bcxj.internalcommon.response.OrderDriverResponse;
import com.bcxj.servicedriveruser.servcie.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserController
 * Package: com.bcxj.servicedriveruser.controller
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/19 11:21
 * @Version 1.0
 * @Since 1.0
 */
@RestController
@RequestMapping
public class DriverUserController {

    @Autowired
    private DriverUserService driverUserService;

    /**
     * 添加用户
     * @return
     */
    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser) {
        return driverUserService.addUser(driverUser);
    }


    /**
     * 更新司机信息
     * @param driverUser
     * @return
     */
    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser) {
        return driverUserService.updateUser(driverUser);
    }


    /**
     *
     * @param driverPhone
     * @return
     */
    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult<DriverUserExistResponse> getUser(@PathVariable String driverPhone) {
        // 调用service服务获取对象
        ResponseResult<DriverUser> driverUserResp = driverUserService.getDriverUserByPhone(driverPhone);

        // 分析获取的对象
        DriverUser driverUser = driverUserResp.getData();
        DriverUserExistResponse driverUserExistResponse = new DriverUserExistResponse();
        Integer ifExist = DriverCarConstants.DRIVER_EXISTS;
        if(null == driverUser) {
            ifExist = DriverCarConstants.DRIVER_NOT_EXISTS;
            driverUserExistResponse.setIfExist(ifExist);
            driverUserExistResponse.setDriverPhone(driverPhone);
        } else {
            driverUserExistResponse.setIfExist(ifExist);
            driverUserExistResponse.setDriverPhone(driverUser.getDriverPhone());
        }

        return ResponseResult.success(driverUserExistResponse);
    }


    /**
     * 根据carId 获取对应的处于出车状态的司机
     * @param carId
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable String carId) {
        return driverUserService.getAvailableDriver(carId);
    }


    /**
     * 根据司机的手机号获取carId的信息
     * @param driverPhone
     * @return
     */
    @GetMapping("driver-car-binding-relationship")
    public ResponseResult<DriverCarBindingRelationship> getDriverCarBindingRelationship(@RequestParam String driverPhone) {
        return driverUserService.getDriverCarBindingRelationship(driverPhone);
    }

}
